<?php
namespace Modules\Admin\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Admin\Http\Requests\AdminCreateRequest;
use Modules\Admin\Http\Requests\AdminUpdateRequest;
use Modules\Admin\Http\Requests\CommonStatusRequest;
use Modules\Admin\Http\Requests\CommonDeleteRequest;
use Modules\Admin\Models\Admin as AdminModel;
use Modules\Admin\Models\AuthGroup as AuthGroupModel;
use Modules\Admin\Models\BaseModel;
use Modules\Admin\Http\Controllers\CommonController;
class AdminController extends CommonController
{
	/**
	 * @name  列表
	 */
    public function index()
    {
		$status = AdminModel::common_status();  
		$auth_groups = AuthGroupModel::orderBy('id','desc')->select('id','title')->get();	
        return view('admin::admin.index',compact('status','auth_groups'));
    }
	/**
	 * @name  列表数据
	 */
    public function data(Request $request)
    {
		$model = AdminModel::query();
		$params = $request->all();
		$model = query_condition($model,$params,'username');
		if (isset($params['group_id']) && $params['group_id'] > 0){
            $model = $model->where('group_id',$params['group_id']);
        }	
        $res = $model->with('auth_groups')
				->orderBy('id','desc') 
				->paginate($params['limit'])
				->toArray(); 
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'count' => $res['total'],'data'  => $res['data'],'rel'=>1]);
    }
	/**
	 * @name  添加页面
	 */
    public function create()
    {
		$auth_groups = AuthGroupModel::orderBy('id','desc')->select('id','title')->get(); 
		return view('admin::admin.create',compact('auth_groups'));
    }
	/**
	 * @name  添加方法数据处理
	 */
    public function store(AdminCreateRequest $request)
    {
		$data = $request->all();
		$data['uuid'] = \Faker\Provider\Uuid::uuid();
        $data['password'] = bcrypt($data['password']);
        unset($data['password_confirmation']);
		return (new AdminModel())->admin_create($data,route('admin.admin.index'));
    }
	/**
	 * @name  编辑页面
	 */
    public function edit($id)
    {
		$info = AdminModel::find($id);
		$auth_groups = AuthGroupModel::orderBy('id','desc')->select('id','title')->get();
		return view('admin::admin.edit',compact('auth_groups','info'));
    }
	/**
	 * @name  编辑数据处理
	 */
    public function update(AdminUpdateRequest $request,$id)
    {
		$data = $request->all(); 
        $data['password'] = bcrypt($data['password']);
        unset($data['password_confirmation']);
		return (new AdminModel())->admin_update($data,$id,route('admin.admin.index')); 
    }
	/**
	 * @name  调整状态数据处理
	 */
    public function status(CommonStatusRequest $request,$id)
    {
		return (new AdminModel())->admin_status_update($request->all(),$id,route('admin.admin.index')); 
    }
	/**
	 * @name  删除数据处理
	 */
    public function destroy(CommonDeleteRequest $request)
    {
		return (new AdminModel())->admin_destroy($request->get('id'),route('admin.admin.index'));  
    }
	/**
	 * @name  初始化管理员密码
	 */
    public function update_pwd(CommonDeleteRequest $request,$id)
    {
		return (new AdminModel())->admin_status_update(['password'=>bcrypt('123456')],$id,route('admin.admin.index'),'初始化成功！','初始化失败，请重试！');  
    }
	
	
	
	
	
	
	
}
